package com.luculent.base;

import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

public abstract class Send {
	
	private static final int  MAX_TIMEOUT = 7000;  
	
	public HttpClient getClient(){ 
		return  HttpClients.createDefault();
	}
	public HttpClient getClientSSL(){
		RequestConfig.Builder configBuilder = RequestConfig.custom();   
	    // 设置连接超时  
	    configBuilder.setConnectTimeout(MAX_TIMEOUT);  
	    // 设置读取超时  
	    configBuilder.setSocketTimeout(MAX_TIMEOUT);  
	    // 设置从连接池获取连接实例的超时  
	    configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);   
	    RequestConfig   requestConfig = configBuilder.build();  
		return  HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setDefaultRequestConfig(requestConfig).build();  
	}
	
	public  String  showCookies(Header[] headers){
		StringBuffer result= new StringBuffer();
		for(Header h : headers){  
			String cookie = h.getName(); 
			if("Set-Cookie".equalsIgnoreCase(cookie)){ 
				for(HeaderElement e : h.getElements()){ 
					result.append(e.getName()).append("=").append(e.getValue()).append(";");
				} 
			} 
		} 
		
		return result.toString();
	}
	public  String  getCookies(HttpServletRequest request){
		StringBuffer result= new StringBuffer();
		Cookie allCookie[]= request.getCookies();
		if(allCookie!=null&&allCookie.length!=0){
			for(int i=0;i<allCookie.length;i++){
	          String keyname= allCookie[i].getName();
	          String keyval= allCookie[i].getValue();
	          result.append(keyname).append("=").append(keyval).append(";"); 
			}
		 }
		return result.toString();
	}
	private  SSLConnectionSocketFactory createSSLConnSocketFactory() {  
        SSLConnectionSocketFactory sslsf = null;  
        try {  
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {  
  
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
                    return true;  
                }  
            }).build();  
            sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() {  
  
                @Override  
                public boolean verify(String arg0, SSLSession arg1) {  
                    return true;  
                }  
            });  
        } catch (GeneralSecurityException e) {  
             
        }  
        return sslsf;  
    }  
}


 